
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const _ = cloud.database().command
export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {
   *     "_id":"12",
   *     "inputType": 0,
   *     "selectList": "颠三倒四",
   * }
   */
  if (nw.util.isNull(body._id)){
    return fail("数据不合法")
  }
  let dataJson = {}
  if (!nw.util.isNull(body.name)){
    //校验名字是否重复
    let count = await nw.db.count({
      dbName:"attr",
      whereJson:{name:_.eq(body.name),deleted:0,_id:_.neq(body._id)}
    })
    if (count>0){
      return fail("分类名字重复，无法修改")
    }
    // @ts-ignore
    dataJson.name = body.name
  }
  if (!nw.util.isNull(body.inputType)){
    // @ts-ignore
    dataJson.inputType = body.inputType
  }
  if (!nw.util.isNull(body.selectList)){
    // @ts-ignore
    dataJson.selectList = body.selectList
  }

  let count = await nw.db.update({
    dbName:"attr",
    whereJson:{_id:body._id},
    dataJson:dataJson
  })
  if (count===0){
    return fail("修改失败")
  }
  return ok("修改成功")
}
